package cn.tedu.eb_film.controller;

import cn.tedu.eb_film.pojo.dto.EmployeeAddNewDTO;
import cn.tedu.eb_film.pojo.dto.EmployeeUpdateDTO;
import cn.tedu.eb_film.pojo.vo.EmployeeLikeNameVO;
import cn.tedu.eb_film.pojo.vo.EmployeeListItemVO;
import cn.tedu.eb_film.pojo.vo.EmployeeStandardVO;
import cn.tedu.eb_film.service.EmployeeService;
import cn.tedu.eb_film.utils.JsonPage;
import cn.tedu.eb_film.utils.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 处理员工相关请求的控制器类
 *
 * @author zzzzc
 * @version 0.0.1
 */

@Api(tags = "员工管理模块")
@Slf4j
@RestController
@RequestMapping("/employees")
@Validated
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    public EmployeeController(){
        log.debug("创建控制器类对象：EmployeeController");
    }

    // http://localhost:8888/employees/add-new
    @PostMapping("/add-new")
    @ApiOperation("添加员工")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid EmployeeAddNewDTO employeeAddNewDTO){
        log.debug("开始处理【添加员工】的请求，参数：{}",employeeAddNewDTO);
        employeeService.addNew(employeeAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("删除员工")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "相册ID", required = true, dataType = "long")
    public JsonResult<Void> delete(@PathVariable@Range(min = 1,message = "请提交有效的id")Long id){
        employeeService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改员工")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "员工ID", required = true, dataType = "long")
    public JsonResult<Void> updateInfoById(@PathVariable@Range(min = 1,message = "请提交有效的id")Long id,
                                           @Valid EmployeeUpdateDTO employeeUpdateDTO){
        log.debug("开始处理【修改员工详情】的请求，ID：{}，新数据：{}", id, employeeUpdateDTO);
        employeeService.updateInfoById(id,employeeUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}/select")
    @ApiOperation("根据id查询员工")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id", value = "员工ID", required = true, dataType = "long")
    public JsonResult<EmployeeStandardVO> getStandardById(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id){
        log.debug("开始处理【根据ID查询员工详情】的请求，参数：{}", id);
        EmployeeStandardVO standardById = employeeService.getStandardById(id);
        return JsonResult.ok(standardById);
    }

    @GetMapping("/list")
    @ApiOperation("查询员工列表")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码",name="page",example = "1"),
            @ApiImplicitParam(value = "每页条数",name="pageSize",example = "2")
    })
    public JsonResult<List<EmployeeListItemVO>> list(){
//        log.debug("开始处理【查询员工列表】的请求，无参数");
//        List<EmployeeListItemVO> list = employeeService.list();
        List<EmployeeListItemVO> list=
                employeeService.list();
        return JsonResult.ok(list);
    }

    @PostMapping("/{id:[0-9]+}/enable")
    @ApiOperation("启用类别")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setEnable(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【启用类别】的请求，参数：{}", id);
        employeeService.setEnable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/disable")
    @ApiOperation("禁用类别")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setDisable(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【禁用类别】的请求，参数：{}", id);
        employeeService.setDisable(id);
        return JsonResult.ok();
    }

    @GetMapping("/namelist")
    @ApiOperation("查询员工姓名列表")
    @ApiOperationSupport(order = 410)
    public JsonResult<List<EmployeeLikeNameVO>> nameList(@RequestParam(name = "username") String likeName){
        log.debug("开始处理【查询员工列表】的请求，参数:{}",likeName);
        List<EmployeeLikeNameVO> nameList = employeeService.nameList(likeName);
        return JsonResult.ok(nameList);
    }
}
